package com.stores.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.stores.action.ManageCategoryAction;
import com.stores.model.Category;
import com.stores.toolset.InputValidator;
import com.stores.toolset.WebAction;

public class CategoryController extends HttpServlet {

	private static final String ADMIN_CATEGORY_SAVE = "/admin/category/save";
	private static final String ADMIN_CATEGORY_LOAD = "/admin/category/load";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ServletContext context = getServletContext();
		String requestUrl = request.getRequestURL().toString();
		if (requestUrl.indexOf("manage-categories") > -1) {
			RequestDispatcher dispatcher = context
					.getRequestDispatcher("/admin/manage-category.jsp");
			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		} else {
			try {
				response.sendError(response.SC_BAD_REQUEST,
						"Valid url is /admin/manage-categories");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
	}

	/***
	 * 
	 */
	@SuppressWarnings("static-access")
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		WebAction webAction = new WebAction(request, response);
		ManageCategoryAction manage = new ManageCategoryAction();
		InputValidator validator = new InputValidator();

		if (webAction.isUrl(ADMIN_CATEGORY_LOAD)) {
			Gson gson = new Gson();
			webAction.writeResponse(gson.toJson(manage.Retrive()));
		} else {
			String description = request.getParameter("category-desc");
			String name = request.getParameter("category-name");
			String parentId = request.getParameter("category-parent");

			if (validator.isValid(description + name + parentId)) {// club them
																	// together
																	// and
																	// send for
																	// validation
				Category category = new Category();
				category.setDescription(description);
				category.setName(name);
				category.setParentId(Long.parseLong(parentId));
				if (webAction.isUrl(ADMIN_CATEGORY_SAVE)) {
					String categoryId = request.getParameter("category-id");
					if (categoryId.length() == 0) {
						manage.Create(category);
					} else {
						category.setId(Long.parseLong(categoryId));
						manage.Update(category);
					}
				}
			} else {
				try {
					response.sendError(response.SC_BAD_REQUEST,
							"Invalid characters found in the input data. Data validation failed!!");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}
		}
	}
}
